package com.silverbox.waimai.service.order.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.silverbox.core.util.Utils;
import com.silverbox.sweeporder.controller.waimai.outvo.AllOrdersByShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.silverbox.core.util.DateUtils;
import com.silverbox.waimai.entity.MsgRecord;
import com.silverbox.waimai.entity.ReceiptTimeoutWaimai;
import com.silverbox.waimai.entity.Waimai;
import com.silverbox.waimai.entity.WaimaiBath;
import com.silverbox.waimai.entity.WaimaiOperateRelated;
import com.silverbox.waimai.entity.WaimaiTimeRelated;
import com.silverbox.waimai.entity.extend.AlreadyGoods;
import com.silverbox.waimai.entity.extend.CopyGoods;
import com.silverbox.waimai.entity.extend.WaimaiExtend;
import com.silverbox.waimai.entity.extend.WaimaiParam;
import com.silverbox.waimai.mapper.MsgRecordMapper;
import com.silverbox.waimai.mapper.ReceiptTimeoutWaimaiMapper;
import com.silverbox.waimai.mapper.WaimaiBathMapper;
import com.silverbox.waimai.mapper.WaimaiMapper;
import com.silverbox.waimai.mapper.WaimaiOperateRelatedMapper;
import com.silverbox.waimai.mapper.WaimaiTimeRelatedMapper;
import com.silverbox.waimai.service.order.WaimaiService;

@Service
public class WaimaiServiceImpl implements WaimaiService {

	@Autowired
	private WaimaiMapper waimaiMapper;
	
	@Autowired
	private WaimaiTimeRelatedMapper waimaiTimeRelatedMapper;
	
	@Autowired
	private WaimaiOperateRelatedMapper 	waimaiOperateRelatedMapper;
	
	@Autowired
	private WaimaiBathMapper waimaiBathMapper;
	
	@Autowired
	private ReceiptTimeoutWaimaiMapper receiptTimeoutWaimaiMapper;
	
	@Autowired
	private MsgRecordMapper msgRecordMapper;
	
	
	@Override
	public void addWaimai(Waimai waimai) {
		waimaiMapper.insert(waimai);
	}

	@Override
	public WaimaiExtend getWaimaiAndDetailByOrderNum(String orderNum) {
		return waimaiMapper.getWaimaiAndDetailByOrderNum(orderNum);
	}

	@Override
	public Waimai getWaimaiByOrderNum(String orderNum) {
		return waimaiMapper.getWaimaiByOrderNum(orderNum);
	}

	@Override
	public int updateWaimai(Waimai waimai) {
		return waimaiMapper.updateByPrimaryKeySelective(waimai);
	}

	@Override
	public Waimai getWaimai(WaimaiParam waimaiParam) {
		return waimaiMapper.getWaimai(waimaiParam);
	}

	@Override
	public Waimai getPreparationWaimai(WaimaiParam waimaiParam) {
		return waimaiMapper.getPreparationWaimai(waimaiParam);
	}

	@Override
	public WaimaiExtend getWaimaiAndDetail(WaimaiParam waimaiParam) {
		return waimaiMapper.getWaimaiAndDetail(waimaiParam);
	}

	@Override
	public List<WaimaiExtend> getHistoryWaimaiList(WaimaiParam waimaiParam) {
		return waimaiMapper.getHistoryWaimaiList(waimaiParam);
	}

	@Override
	public void updateWaimaiItem(Long id) {
		waimaiMapper.updateWaimaiItem(id);
	}

	@Override
	public void deleteOrderByIds(List<String> ids) {
		waimaiMapper.deleteOrderByIds(ids);
	}

	@Override
	public List<CopyGoods> getCopyGoods(Integer storeId, String tableUid, String startDate, String endDate) {
		return waimaiMapper.getCopyGoods(storeId, tableUid, startDate, endDate);
	}
	
	@Override
	public void deleteOrderByOrderNum(List<String> orderNums) {
		waimaiMapper.deleteOrderByOrderNum(orderNums);
	}

	@Override
	public int deleteBagOrBoxItemByWaimaiId(Long waimaiId) {
		return waimaiMapper.deleteBagOrBoxItemByWaimaiId(waimaiId);
	}

	@Override
	public int updateWaimaiByCleanTable(WaimaiParam waimai) {
		return waimaiMapper.updateWaimaiByCleanTable(waimai);
	}

	@Override
	public List<AlreadyGoods> getAlreadyGoods(Integer storeId,Long thirdUserInfoId,String createDate) {
		return waimaiMapper.getAlreadyGoods(storeId,thirdUserInfoId,createDate);
	}

	@Override
	public List<WaimaiExtend> getOnlineOrderHexiao(WaimaiParam param) {
		List<WaimaiExtend> waimaiList = waimaiMapper.getOnlineOrderHexiao(param);
		return waimaiList;
	}

	@Override
	public List<WaimaiExtend> getOnlineMealOrder(WaimaiParam param) {
		 return waimaiMapper.getOnlineMealOrder(param);
	}

	@Override
	public int updateWaimaiIdOrNum(WaimaiParam param) {
		return waimaiMapper.updateWaimaiIdOrNum(param);
	}

	@Override
	public int updateWaimaiByOrderNum(Waimai param) {
		return waimaiMapper.updateWaimaiByOrderNum(param);
	}

	@Override
	public int updateWaimaiTimeRelated(WaimaiTimeRelated timeRelated) {
		return waimaiTimeRelatedMapper.updateByPrimaryKeySelective(timeRelated);
	}

	@Override
	public int saveWaimaiTimeRelated(WaimaiTimeRelated timeRelated) {
		return waimaiTimeRelatedMapper.insertSelective(timeRelated);
	}

	@Override
	public int saveOrUpdateWaimaiBath(WaimaiBath waimaiBath) {
		int count =0;
		if(waimaiBath.getId()!=null){
			count = waimaiBathMapper.updateByPrimaryKeySelective(waimaiBath);
		}else{
			count = waimaiBathMapper.insertSelective(waimaiBath);
		}
		return count;
	}

	@Override
	public int saveReceiptTimeoutWaimai(ReceiptTimeoutWaimai receiptWaimai) {
		return receiptTimeoutWaimaiMapper.insertSelective(receiptWaimai);
	}

	@Override
	public ReceiptTimeoutWaimai getReceiptTimeoutWaimai(String orderNum) {
		return receiptTimeoutWaimaiMapper.getReceiptTimeoutWaimai(orderNum);
	}

	@Override
	public int saveWaimaiOperateRelated(WaimaiOperateRelated waimaiOperateRelated) {
		return waimaiOperateRelatedMapper.insertSelective(waimaiOperateRelated);
	}

	public List<WaimaiExtend> getTodayOrderHexiaoOrDelivery(WaimaiParam param) {
		List<WaimaiExtend> waimaiList = waimaiMapper.getTodayOrderHexiaoOrDelivery(param);
		return waimaiList;
	}

	@Override
	public List<Waimai> getMyUnPayOrder(Integer storeId, String tableName, String buyerId) {
		String startDate = DateUtils.getYesterdayStartTime();
		String endDate = DateUtils.getTodayEndTime();
		return waimaiMapper.getMyUnPayOrder(storeId,tableName,buyerId,startDate,endDate);
	}

	@Override
	public void deleteUnpayHistoryOrderById(Long id) {
		waimaiMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int saveMsgRecord(MsgRecord msgRecord) {
		return msgRecordMapper.insert(msgRecord);
	}
	
	@Override
	public List<MsgRecord> selectMsgRecord(Integer storeId,Long machineId) {
		return msgRecordMapper.selectMsgRecord(storeId,machineId);
	}

	@Override
	public void deleteMsgRecord(String msgCode) {
		msgRecordMapper.deleteMsgRecord(msgCode);
	}

    @Override
    public Map<String,Object> findAllOrdersByShop(Long shopId, String storeName, Integer outSupportId, Integer pageNo, Integer pageSize, List<String> buyerIds) {
        Date dateForXBeaforeMonth = DateUtils.getDateForXBeaforeMonth(new Date(), 1);
        String startTime = new SimpleDateFormat("yyyy-MM-dd").format(dateForXBeaforeMonth);

        if(!Utils.isEmpty(storeName)){
            storeName = "%" + storeName + "%";
        }
        PageHelper.startPage(pageNo,pageSize);
        List<AllOrdersByShopVo> resultList = waimaiMapper.findAllOrdersByShop(shopId,storeName,outSupportId,buyerIds,startTime);
        int count = waimaiMapper.countAllOrdersByShop(shopId,storeName,outSupportId,buyerIds,startTime);

        Map<String, Object> data = new HashMap<>(2);
        data.put("count",count);
        data.put("list",resultList);

        return data;
    }


    @Override
    public Map<String,Object> findAllOrders(String appId, String storeName, Integer outSupportId, Integer pageNo, Integer pageSize, List<String> buyerId) {
	    Date dateForXBeaforeMonth = DateUtils.getDateForXBeaforeMonth(new Date(), 1);
        String startTime = new SimpleDateFormat("yyyy-MM-dd").format(dateForXBeaforeMonth);
        if(!Utils.isEmpty(storeName)){
            storeName = "%" + storeName + "%";
        }
        if(Utils.isEmpty(appId)){
            PageHelper.startPage(pageNo,pageSize);
        }
        List<AllOrdersByShopVo> resultList = waimaiMapper.findAllOrders(storeName,outSupportId,buyerId,startTime);
        Map<String, Object> data = new HashMap<>(2);
        if(Utils.isEmpty(appId)){
            int count = waimaiMapper.countAllOrders(storeName,outSupportId,buyerId,startTime);
            data.put("count",count);
        }
        data.put("list",resultList);
        return data;
    }

    @Override
    public List<WaimaiExtend> getWaimaiByBuyerIdInUnPaidEffective(String buyerId) {
        Date sevenDaysAgoTime = DateUtils.sevenDaysAgoTime();
        return waimaiMapper.getWaimaiByBuyerIdInUnPaidEffective(buyerId,sevenDaysAgoTime);
    }

    public static void main(String[] args) {
        Date dateForXBeaforeMonth = DateUtils.getDateForXBeaforeMonth(new Date(), 1);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(dateForXBeaforeMonth));
    }

}
